<template>
    <div>
        <el-dialog title="添加选树典型" :visible.sync="dialogVisible" width='450px' append-to-body v-dialogDrag :close-on-click-modal="false" >
            <div>
                <el-form ref="rowdata" :model="rowdata" label-width="100px" class="personeditform" :rules="rules" status-icon>
                    <el-form-item label="年份" prop="createtime">
                        <el-date-picker style="width: 80%;" v-model="rowdata.createtime" type="year" placeholder="" value-format="yyyy"></el-date-picker>
                    </el-form-item>
                     <el-form-item label="党委"   prop="dwid">
                        <el-select style="width: 80%;" v-model="rowdata.dwid" @change="changedw">
                            <el-option v-for="item in alldws" :key="item.name" :label="item.name" :value="item.id"></el-option> 
                        </el-select>
                    </el-form-item>
                    <el-form-item label="支部" prop="dzbid">
                        <el-select style="width: 80%;" v-model="rowdata.dzbid">
                            <el-option  v-for="item in allzbs" :key="item.name" :label="item.name" :value="item.id"></el-option> 
                        </el-select>
                    </el-form-item>
                    <el-form-item label="姓名" prop="name">
                        <el-input v-model="rowdata.name" style="width: 80%;" ></el-input>
                    </el-form-item>
                     <el-form-item label="表彰项目"  prop="bzxm">
                        <el-select style="width: 80%;" v-model="rowdata.bzxm" >
                            <el-option label="先进基层党组织" value="先进基层党组织"></el-option>
                            <el-option label="优秀党务工作者" value="优秀党务工作者"></el-option>
                            <el-option label="优秀共产党" value="优秀共产党"></el-option>
                           
                        </el-select>
                    </el-form-item>
                    <el-form-item label="表彰单位" prop="bzdw">
                        <el-input v-model="rowdata.bzdw" style="width: 80%;" ></el-input>
                    </el-form-item>
                    <el-form-item label="表彰时间" prop="bzsj">
                            <el-date-picker v-model="rowdata.bzsj" style="width: 80%;" type="date" placeholder="" value-format="yyyy-MM-dd"></el-date-picker>
                    </el-form-item>
                </el-form>
                <el-row style="position: relative;width: 100%;">
                    <el-button style="margin: 0 auto;display: block;"  type="primary" @click="checksubmit('rowdata')">确定并保存<i class="el-icon-upload el-icon--right" ></i></el-button>
                </el-row>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import Common from "../../../../js/Common.js";
    import {mapActions} from 'vuex';
    export default {
        data() {
            return { 
                dialogVisible: false, 
                rowdata:{
                    createtime:'',
                    dwid:'',
                    dzbid:'',
                    type:'',
                },
                alldws:[],//党内有职务时选择的委员会
                allzbs:[], //党内有职务时选定委员会时出现的党支部

                rules: {
                    createtime: [{ required: true, message: '不能为空', trigger: 'blur' }],
                    dwid: [{ required: true, message: '不能为空', trigger: 'blur' }],
                    dzbid: [{ required: true, message: '不能为空', trigger: 'blur' }],
                    type: [{ required: true, message: '不能为空', trigger: 'blur' }],
                },
            };
            
        },
        mounted() {
            this.alldws=[];
            let _this=this; ;        
            this.GetZbById({dwid:-1,dzbid:-1})
            .then(function(data){
                for (let index = 0; index < data.childs.length; index++) {
                    const element = data.childs[index];
                    _this.alldws.push({
                        id:element.dwid,
                        name:element.alias
                    })
                }
            })
            
        },
        components: {},
        watch: {
            dialogVisible: function (n) {
                if (n == true) {
                }
            },
        },
        methods: {
            ...mapActions(['GetZbById','GetZbByIdstate']),
            changedw(name){
                let finddws=this.alldws.filter(function(valuedw){
                    return valuedw.id==name;
                })
                let dwid=-1;
                if(finddws.length>0){
                    dwid=finddws[0].id;
                }
                else{
                    return;
                }
                let _this=this;
                this.allzbs=[];
                this.GetZbById({dwid:dwid,dzbid:-1})
                .then(function(data){
                    for (let index = 0; index < data.childs.length; index++) {
                        const element = data.childs[index];
                        _this.allzbs.push({
                            id:element.dzbid,
                            name:element.alias
                        })
                    }
                })
                
            },
            checksubmit(formName){
                console.log(this.rowdata);
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        
                        this.uploadToServe();
                    } 
                    else {
                        this.$message("请填写必要信息");
                        return false;
                    }
                });
            },
            uploadToServe(){
                this.uploadAjax();
            },
            uploadAjax(){
                let _this=this;
                let postdata={
                    type:"addfldj",
                    split:JSON.stringify(this.rowdata)
                }
                Common.AjaxPost(postdata,true)
                .then(function(e){
                    _this.$message("操作成功");
                    _this.dialogVisible=false;
                    _this.$emit('editResult');
                },function(){
                     _this.$message("操作失败");
                })
                .catch(function(reason) {
                    console.log('catch:', reason);
                });
            },
        },

    }
</script>
<style>
</style>